<?php
class Model_Khachhang extends Zend_Db_Table_Abstract {
	protected $DataAp;
	private $str_query = "SELECT * ";
	private $str_queryod = "SELECT id,customerName,(DATE_FORMAT(created,'%d/%m/%Y - %H:%i:%p')) AS created,createdBy,fee,total,debit,note,prepaid,promotion,payment,checkmo ";
	public function __construct() {
		$this->DataAp = Zend_Registry::get ( "db" );
	}	
	public function getItems($sort, $order, $offset, $rows) {
		$result = array();
        $query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM customer " );
        $row = $query->fetchAll ();        
		$query = $this->DataAp->query ("SELECT * FROM customer ORDER BY $sort $order LIMIT $offset, $rows " );
		$result ["total"] = $row [0] ['Total'];
		$result ["rows"] = $query->fetchAll ();
		return $result;
	}
	public function getOrdersById($id){
	    $data = array();        
		$query = $this->DataAp->query ( $this->str_queryod . "FROM orders WHERE customerPassport = $id" );
		$data = $query->fetchAll ();
		return $data;
	}
	public function getListHd($id){
	    $data = array();        
		$query = $this->DataAp->query ( " SELECT id , productName,quantity,price, (DATE_FORMAT(startTime,'%d/%m/%Y - %H:%i:%p')) as startTime ,(DATE_FORMAT(endTime,'%d/%m/%Y - %H:%i:%p')) as  endTime,storename, (quantity*price) as tonggiadv FROM `orderproduct` WHERE orderId = $id" );
		$data = $query->fetchAll ();
		return $data;
	}
	public function addObj($data) {
		$Id = $this->DataAp->insert ( 'customer', $data );
		return $Id;
	} 
	public function update($id, $data) {
		$Id = $this->DataAp->update ( 'customer', $data, 'id = ' . $id );
		return $Id;
	}
	public function del($id) {
		$Id = $this->DataAp->delete ( 'customer', 'id = ' . $id );
		return $Id;
	}
	public function dupliObj($id, $ten) {
		$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM customer WHERE passportNumber = '$ten'" );
		if ($id != 0) {
			$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM customer WHERE   id != $id AND passportNumber = '$ten'" );
		}
		$row = $query->fetchAll ();
		$total = $row [0] ['Total'];
		return $total;
	}
	public function searObj($ten ,$socmt, $tu, $den,$sort, $order, $offset, $rows) {
		$result = array ();
		$where = " name LIKE '%$ten%' ";
		if ($socmt != '')
			$where = $where . "AND passport LIKE '%$socmt%' ";
		if ($tu != '' && $den != '')
			$where = $where . "AND created BETWEEN  '$tu' AND '$den' ";
		elseif ($tu != '' || $den != '') {
			if ($tu != '')
				$where = $where . "AND created >= '$tu'";
			else
				$where = $where . "AND created <= '$den'";
		}
		// Lay tong so ban ghi
		$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM customer
				WHERE  $where " );
		$row = $query->fetchAll ();
		$result ["total"] = $row [0] ['Total'];
		// Thuc hien cau lenh truy xuat du lieu
		$query = $this->DataAp->query ( $this->str_query . " FROM customer
						WHERE $where ORDER BY $sort $order LIMIT $offset, $rows" );
		
		$result ['rows'] = $query->fetchAll ();
		return $result;
	}
	
	public function dupmm($cmnd)
	{
		$query = $this->DataAp->query ( "SELECT COUNT(*) AS Total FROM `customer` WHERE passport = $cmnd " );
		$row = $query->fetchAll();
		$total = $row[0]['Total'];
		return $total;
	}
	
	public function thanhtoanno($id, $data) {
		$Id = $this->DataAp->update ( 'orders', $data, 'id = ' . $id );
		return $Id;
	}
}
?>
   
   

